﻿using Questionary.Logic;
using Questionary.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Questionary.Views
{
	public partial class Edit : System.Web.UI.Page
	{

		/// <summary>
		/// 1：客观题，2：主观题
		/// </summary>
		public int EditType = 0;

		public int EditID = -1;

		protected void Page_Load(object sender, EventArgs e)
		{
			//是否修改
			int editIndex = -1;
			if(!string.IsNullOrEmpty(Request["ID"]) && int.TryParse(Request["ID"], out editIndex))
			{
				EditID = editIndex;
			}

			if (!IsPostBack)
				BindData();

		}

		public void BindData()
		{
			if (EditID >= 0)
			{
				QuestionItem qItem = null;
				List<QuestionItem> questions = FileHelper.GetObject<List<QuestionItem>>(Request.MapPath(FilePaths.QuestionFilePath));

				foreach (var item in questions)
				{
					if (item.ID == EditID)
					{
						qItem = item;
					}
				}

				if (qItem != null)
				{
					if (qItem.QType == 2)
						rdText.Checked = true;
					else if (qItem.QType == 1)
						rdMulti.Checked = true;
					else rdSingle.Checked = true;

					txtIdx.Text = qItem.ID.ToString();
					txtDesc.Text = qItem.Description;

					if (qItem.QType < 2)
					{
						if (qItem.Selections.Count > 0)
							TextBox1.Text = qItem.Selections[0].Content;
						if (qItem.Selections.Count > 1)
							TextBox2.Text = qItem.Selections[1].Content;
						if (qItem.Selections.Count > 2)
							TextBox3.Text = qItem.Selections[2].Content;
						if (qItem.Selections.Count > 3)
							TextBox4.Text = qItem.Selections[3].Content;
						if (qItem.Selections.Count > 4)
							TextBox5.Text = qItem.Selections[4].Content;
						if (qItem.Selections.Count > 5)
							TextBox6.Text = qItem.Selections[5].Content;
						if (qItem.Selections.Count > 6)
							TextBox7.Text = qItem.Selections[6].Content;
						if (qItem.Selections.Count > 7)
							TextBox8.Text = qItem.Selections[7].Content;

					}
				}
			}
		}

		protected void btnSaveItem_Click(object sender, EventArgs e)
		{
			QuestionItem qItem = null;

			List<QuestionItem> questions = FileHelper.GetObject<List<QuestionItem>>(Request.MapPath(FilePaths.QuestionFilePath));
			if (questions == null)
				questions = new List<QuestionItem>();


			if(EditID > -1)
			{
				//找到当前题目对象
				foreach (var qi in questions)
				{
					if (EditID == qi.ID)
					{
						qItem = qi;
					}
				}
			}
			else
			{
				qItem = new QuestionItem();
			}


			if (qItem == null)
			{
				Response.End();
				return;
			}

			qItem.ID = int.Parse(txtIdx.Text);
			qItem.Description = txtDesc.Text.Trim();
			qItem.QType = int.Parse(Request.Form["qType"]);
			qItem.Selections = new List<SelectionItem>();

			//客观题
			if (qItem.QType < 2)
			{	
				if (!string.IsNullOrEmpty(TextBox1.Text.Trim()))
				{
					qItem.Selections.Add(new SelectionItem() { Content = TextBox1.Text, ID = qItem.Selections.Count });
				}

				if (!string.IsNullOrEmpty(TextBox2.Text.Trim()))
				{
					qItem.Selections.Add(new SelectionItem() { Content = TextBox2.Text, ID = qItem.Selections.Count });
				}

				if (!string.IsNullOrEmpty(TextBox3.Text.Trim()))
				{
					qItem.Selections.Add(new SelectionItem() { Content = TextBox3.Text, ID = qItem.Selections.Count });
				}

				if (!string.IsNullOrEmpty(TextBox4.Text.Trim()))
				{
					qItem.Selections.Add(new SelectionItem() { Content = TextBox4.Text, ID = qItem.Selections.Count });
				}

				if (!string.IsNullOrEmpty(TextBox5.Text.Trim()))
				{
					qItem.Selections.Add(new SelectionItem() { Content = TextBox5.Text, ID = qItem.Selections.Count });
				}

				if (!string.IsNullOrEmpty(TextBox6.Text.Trim()))
				{
					qItem.Selections.Add(new SelectionItem() { Content = TextBox6.Text, ID = qItem.Selections.Count });
				}

				if (!string.IsNullOrEmpty(TextBox7.Text.Trim()))
				{
					qItem.Selections.Add(new SelectionItem() { Content = TextBox7.Text, ID = qItem.Selections.Count });
				}

				if (!string.IsNullOrEmpty(TextBox8.Text.Trim()))
				{
					qItem.Selections.Add(new SelectionItem() { Content = TextBox8.Text, ID = qItem.Selections.Count });
				}

			}

			if (EditID > -1)
			{
				questions.Remove(qItem);
			}

			questions.Insert(0, qItem);
			var orderList = questions.OrderBy(tmp => tmp.ID).ToList();
			foreach (var item in questions)
			{
				item.ID = orderList.IndexOf(item) + 1;
			}
			FileHelper.WriteObject<List<QuestionItem>>(orderList, Request.MapPath(FilePaths.QuestionFilePath));

			Response.Write("保存成功");
			Response.Redirect("QuestionManager.aspx");
		}
	}
}